<template>
<div class="banner-list">
  <div style="display:flex;align-items: center;justify-content: space-between;margin:0 0 10px;">
    <div style="display:flex;align-items: center;">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="名称">
          <el-input clearable size="small" v-model="formInline.title" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select clearable size="small" v-model="formInline.state" placeholder="请选择状态">
            <el-option
              v-for="item in [{
                value: '1',
                label: '启用'
              },{
                value: '0',
                label: '禁用'
              },]"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
        <el-button size="small" type="primary" @click="onSubmit">搜索</el-button>
        <el-button size="small" @click="reset">重置</el-button>
      </div>
      <div>
        <el-button size="small" type="primary" @click="add">新建工具</el-button>
      </div>
  </div>
  <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
    <el-table-column label="ID" width="150">
      <template slot-scope="scope">
        {{ scope.row.id }}
      </template>
    </el-table-column>
    <el-table-column label="名称" width="200">
      <template slot-scope="scope">
        {{ scope.row.name }}
      </template>
    </el-table-column>
    </el-table-column>
    <el-table-column label="简介" width="200">
      <template slot-scope="scope">
        {{ scope.row.abbr }}
      </template>
    </el-table-column>
    <el-table-column label="APPID" width="180">
      <template slot-scope="scope">
        {{ scope.row.appId }}
      </template>
    </el-table-column>
    <el-table-column label="跳转地址" width="180">
      <template slot-scope="scope">
        {{ scope.row.redirectUrl }}
      </template>
    </el-table-column>
    <el-table-column label="缩略图" width="180">
      <template slot-scope="scope">
        <img :src="scope.row.thumbnail" alt="" style="width:80px;height:40px;">
      </template>
    </el-table-column>
    <el-table-column label="排序" width="200">
      <template slot-scope="scope">
        {{ scope.row.seqNo }}
      </template>
    </el-table-column>
    <el-table-column label="应用类型" width="180">
      <template slot-scope="scope">
        <el-tag size="small" v-if="scope.row.appType == 1" type="success">微信小程序</el-tag>
        <el-tag size="small" v-else type="primary">微信公众号</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="状态" width="100">
      <template slot-scope="scope">
        <el-tag size="small" v-if="scope.row.status == 0" type="danger">禁用</el-tag>
        <el-tag size="small" v-else type="success">启用</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="创建时间" width="180">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
    <el-table-column fixed="right" label="操作" width="150">
      <template slot-scope="scope">
        <el-button @click="changeState(scope.row)" type="text" style="color:#ff0000;" v-if="scope.row.status == 1">禁用</el-button>
        <el-button @click="changeState(scope.row)" type="text" v-else>启用</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-button @click="editToolRow(scope.row)" type="text">编辑</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-popconfirm
          @onConfirm="del(scope.row)"
          confirm-button-text='确定'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="是否删除本条数据？">
          <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div style="margin:10px 0 0 0;text-align:right;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="1"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageObj.total">
    </el-pagination>
  </div>

  <el-drawer
    title="配置工具"
    :visible.sync="drawerVisible"
    direction="rtl"
    size="60%"
    :before-close="handleClose">
    <div style="padding: 0 40px;height: 100%;position: relative;">
      <div style="display:flex;align-items:center;margin-bottom: 20px;">
        <div style="width: 100px;">工具名称：</div>
        <div style="flex: 1">
          <el-input clearable v-model="toolObj.name" placeholder="请输入"></el-input>
        </div>
      </div>
      <div style="display:flex;margin-bottom: 20px;">
        <div style="width: 100px;">缩略图：</div>
        <div style="flex: 1">
          <el-upload
            :class="imgFilesList.length?'hiddenUpload':''"
            action="https://service.bairicun.com/file/upload"
            list-type="picture-card"
            :on-success="handleSuccess"
            :on-preview="handlePictureCardPreview"
            :file-list="imgFilesList"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
        </div>
      </div>
      <div style="display:flex;margin-bottom: 20px;">
        <div style="width: 100px;">工具简介：</div>
        <div style="flex: 1">
          <el-input type="textarea" :rows="4" clearable v-model="toolObj.abbr" placeholder="请输入"></el-input>
        </div>
      </div>
      <div style="display:flex;align-items:center;margin-bottom: 20px;">
        <div style="width: 100px;">应用类型：</div>
        <div style="flex: 1">
          <el-radio-group v-model="toolObj.appType">
            <el-radio :label="1">微信小程序</el-radio>
            <el-radio :label="0">微信公众号</el-radio>
          </el-radio-group>
        </div>
      </div>
      <div style="display:flex;align-items:center;margin-bottom: 20px;">
        <div style="width: 100px;">APPID：</div>
        <div style="flex: 1">
          <el-input clearable v-model="toolObj.appId" placeholder="请输入"></el-input>
        </div>
      </div>
      <div style="display:flex;align-items:center;margin-bottom: 20px;">
        <div style="width: 100px;">跳转地址：</div>
        <div style="flex: 1">
          <el-input clearable v-model="toolObj.redirectUrl" placeholder="请输入"></el-input>
        </div>
      </div>
      <div style="display:flex;align-items:center;margin-bottom: 20px;">
        <div style="width: 100px;">排序：</div>
        <div style="flex: 1">
          <el-input clearable v-model="toolObj.seqNo" placeholder="请输入"></el-input>
        </div>
      </div>
      <div style="display:flex;align-items:center;margin-bottom: 40px;">
        <div style="width: 100px;">状态类型：</div>
        <div style="flex: 1">
          <el-radio-group v-model="toolObj.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </div>
      </div>
      <div style="display:flex;align-items:center;margin-bottom: 20px;justify-content: flex-end;">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button v-if="toolObj.id" @click="editTool" type="primary">修 改</el-button>
        <el-button v-else @click="addTool" type="primary">添 加</el-button>
      </div>
    </div>
  </el-drawer>

  <el-dialog :visible.sync="dialogVisible">
    <img v-if="imgFilesList[0] && imgFilesList[0].url" width="100%" :src="imgFilesList[0].url" alt="">
  </el-dialog>
</div>
</template>

<script>
import { getToolListAllApi, deleteToolAllApi, addToolAllApi, editToolAllApi } from '@/api/tool'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      listLoading: true,
      currentPage: 1,
      formInline: {
        title: '',
        state: '',
      },
      pageObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      drawerVisible: false,
      operateType: 1,
      statusType: 1,
      imgFilesList: [],
      dialogVisible: false,
      toolObj: {
        id: '',
        name: '',
        abbr: '',
        redirectUrl: '',
        appId: '',
        appType: 1,
        status: 1,
        seqNo: '',
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    editToolRow(row) {
      this.toolObj.id = row.id
      this.toolObj.name = row.name
      this.toolObj.abbr = row.abbr
      this.toolObj.redirectUrl = row.redirectUrl
      this.toolObj.appId = row.appId
      this.toolObj.appType = row.appType
      this.toolObj.status = row.status
      this.toolObj.seqNo = row.seqNo
      this.imgFilesList = [
        {
          url: row.thumbnail,
          id: 1
        }
      ]
      this.drawerVisible = true
    },
    editTool() {
      if(!(this.imgFilesList[0] && this.imgFilesList[0].url)) {
        this.$message.error('请上传图片！');
        return
      }
      editToolAllApi({
        id: this.toolObj.id,
        name: this.toolObj.name,
        abbr: this.toolObj.abbr,
        redirectUrl: this.toolObj.redirectUrl,
        appId: this.toolObj.appId,
        appType: this.toolObj.appType,
        status: this.toolObj.status,
        seqNo: this.toolObj.seqNo,
        thumbnail: this.imgFilesList[0].url
      }).then((res) => {
        this.$message.success('操作成功！');
        this.fetchData()
        this.closeDialog()
      })
    },
    addTool() {
      if(!(this.imgFilesList[0] && this.imgFilesList[0].url)) {
        this.$message.error('请上传图片！');
        return
      }
      addToolAllApi({
        name: this.toolObj.name,
        abbr: this.toolObj.abbr,
        redirectUrl: this.toolObj.redirectUrl,
        appId: this.toolObj.appId,
        appType: this.toolObj.appType,
        status: this.toolObj.status,
        seqNo: this.toolObj.seqNo,
        thumbnail: this.imgFilesList[0].url
      }).then((res) => {
        this.$message.success('操作成功！');
        this.fetchData()
        this.closeDialog()
      })
    },
    closeDialog() {
      this.drawerVisible = false
      this.toolObj.id = ''
      this.toolObj.name = ''
      this.toolObj.abbr = ''
      this.toolObj.redirectUrl = ''
      this.toolObj.appId = ''
      this.toolObj.appType = 1
      this.toolObj.status = 1
    },
    handleSuccess(e) {
      if(e.code == 200) {
        this.imgFilesList = [
          {
            url: e.data.url,
            id: e.data.id,
          }
        ]
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleRemove() {
      this.imgFilesList = []
    },
    handleClose() {
      this.drawerVisible = false
    },
    add() {
      this.drawerVisible = true
    },
    onSubmit() {
      this.fetchData()
    },
    del(row) {
      deleteToolAllApi({
        id: row.id
      }).then((res) => {
        this.fetchData()
      })
    },
    reset() {
      this.pageObj.pageSize = 10
      this.pageObj.pageNum = 1
      this.formInline.title = ''
      this.formInline.state = ''
      this.fetchData()
    },
    edit(row) {
      this.$router.push({
        path: '/banner/edit',
        query: {
          editId: row.id
        }
      })
    },
    changeState(row) {
      editToolAllApi({
        id: row.id,
        status: row.status == 1? 0: 1,
      }).then((res) => {
        this.$message.success('操作成功！');
        this.fetchData()
      })
    },
    fetchData() {
      this.listLoading = true
      // getListApi({
      //   title: this.formInline.title,
      //   state: this.formInline.state,
      //   pageSize: this.pageObj.pageSize,
      //   pageNum: this.pageObj.pageNum,
      // }).then(response => {
      //   this.list = response.data.result
      //   this.pageObj.total = response.data.total
      //   this.listLoading = false
      // })
      getToolListAllApi({
        name: this.formInline.title,
        status: this.formInline.state,
        pageSize: this.pageObj.pageSize,
        pageNum: this.pageObj.pageNum
      }).then((res) => {
        this.list = res.data.list
        this.pageObj.total = res.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageObj.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageObj.pageNum = val
      this.fetchData()
    }
  }
}
</script>

<style>
  .hiddenUpload .el-upload--picture-card{
    display:none !important;
  }
</style>
<style lang="scss" scoped>
   .banner-list{
     padding: 20px;
     .el-form-item{
       margin-bottom: 0;
     }
   }
</style>
